package cate.game.role.bag.inscription;

import cate.common.util.GameResult;
import cate.game.attr.InscriptionPowerAttrPart;
import cate.game.attr.PowerAttrPart;
import cate.game.event.hero.HeroPowerChangeEvent;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.bag.hero.Hero;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Transient;

public class InscriptionPageOn extends RoleDependent {
    @JsonIgnore
    @Transient
    public Hero owner;

    @NoteField("英雄身上的铭文页id")
    public int page;

    public void initialize(Hero owner, Role role) {
        super.initialize(role);
        this.owner = owner;
    }


    @JsonIgnore
    public InscriptionPage loadPage() {
        return role.getRes().inscription.pageMap.get(this.page);
    }

    public void doRealOn(InscriptionPage ipage) {
        this.page= ipage.id;
        if (loadPage() == null) {
            return;
        }
        for (String uid :
                loadPage().owners) {
            if (StringUtils.equals(owner.uid, uid)) {
                return;
            }
        }
        loadPage().owners.add(owner.uid);
    }

    public void doRealOff() {
        if (this.page > 0 && role.getRes().inscription.pageMap.containsKey(this.page)) {
            loadPage().owners.removeIf(uid -> StringUtils.equals(owner.uid, uid));
        }
        this.page = 0;
    }

    @JsonIgnore
    public PowerAttrPart getPowerAttrPart(PlayBuildContext ctx) {
        return new InscriptionPowerAttrPart(ctx,this);
    }

    public InscriptionPageOn copy() {
        InscriptionPageOn pageOn = new InscriptionPageOn();
        pageOn.initialize(this.owner, this.role);
        pageOn.page = this.page;
        return pageOn;
    }

    //英雄装备铭文页
    public GameResult<Hero> on(InscriptionPage ipage, Hero hero, boolean updatePower) {
        GameResult<Hero> r = new GameResult<>();
        r.data = hero;
        if (ipage == null) {
            return r.fail("铭文页不存在");
        }
        if (hero.lock.heroPendant) {
            return r.fail("食灵在聚灵树中");
        }
        if (ipage.id == this.page) {
            return r;
        }

        if (role.getRes().inscription.pageMap.get(this.page) != null) {
            GameResult<Hero> off = off(hero, false);
            if (!off.ok()) {
                return off;
            }
        }
        doRealOn(ipage);
        if (updatePower) {
            hero.getRole().getBase().updatePower();
            role.getGame().eventDispatcher.dispatchEvent(new HeroPowerChangeEvent(hero.getRole(), hero));
        }
        return r;
    }

    public GameResult<Hero> off(Hero hero, boolean updatePower) {
        GameResult<Hero> r = new GameResult<>();
        if (role.getRes().inscription.pageMap.get(this.page) == null) {
            return r.fail("英雄未装备铭文页");
        }
        doRealOff();
        if (updatePower) {
            hero.getRole().getBase().updatePower();
            role.getGame().eventDispatcher.dispatchEvent(new HeroPowerChangeEvent(hero.getRole(), hero));
        }
        return r;
    }

}
